package com.kx.springmvc.controller;

import com.kx.springmvc.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DataSourceController
 * @Author Sun
 * @Date 2021/5/19 17:30
 */

/**
 * @ResponseBody 注解作用将方法的返回结果作为响应正文,如果返回结果是 String 则原样返回,
 * 如果返回的是一个对象,springmvc 内部的解析器默认的解析方式是转成 json之后作为响应正文返回
 */
@ResponseBody
@RequestMapping("datas")
@Controller
public class DataController {

    @RequestMapping("/resp1")
    public String respString(String name){
        return name;
    }

    @RequestMapping("resp2")
    public Map respMap(){
        HashMap map = new HashMap();
        map.put("name", "张三");
        map.put("age", 18);
        map.put("sex", "男");
        return map;
    }

    @RequestMapping("resp3")
    public User respUser(){
        User user = new User();
        user.setUsername("冰冰");
        user.setPassword("123456");
        user.setAge(0);
        user.setSex("女");
        user.setAddress("上海");
        return user;
    }

    @RequestMapping("resp4")
    public List<User> respList(){
        ArrayList<User> users = new ArrayList<>();
        User user = new User();
        for (int i = 0; i < 10; i++) {
            user.setUsername("冰冰"+i);
            user.setPassword("123456");
            user.setAge(0);
            user.setSex("女");
            user.setAddress("上海"+i);
            users.add(user);
        }
        return users;
    }
}
